import imgUrl from "../../images/util.js"
import { filteremoji } from "../../utils/index.js"
import {newAddStaff} from "../../pages/pageGlobalData.js"
import { addEmployee} from "../../ajax/module/userInfo.js"

let app=getApp()
Page({
    data:{
        isNeedIcon:imgUrl.isNeedIcon,
        sexArr: ['女', '男'],
        education:["小学","初中","高中","中专","高技","专科","本科"],

        date:"请选择日历",
        name:"",
        phone:"",
        sexSelect:"请选择性别",
        currentDate:"",

        type:"", //保存类型
        text:"保存",
       
        id:""
      
    },
    onUnload(){
        console.log("5665")
        if(this.data.id ==1){
            app.globalData.newAddStaff={}
        }
    },
    getCurrentDate(){
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
        }
        var currentdate =  year + seperator1 + month + seperator1 + strDate +"";
        return currentdate;
    },
    onLoad(option){
        console.log("option",option.id) 

        let sex
        let {employeeName,employeePhone,gender,birthday,education} =app.globalData.newAddStaff
       
        if(gender == 0 ){
             sex= "女"
        }else if(gender == 1){
             sex="男" 
        }else{
            sex="请选择性别"
        }

       
      
        this.setData({
            id:option.id,
            name:employeeName||"",
            phone:employeePhone||"",
            sexSelect:sex,
            date:birthday||"请选择日历",
            educationSelect:education||"请选择学历",
            type:option.id,
            text:(option.id==2)?"新增":"保存",
            currentDate:this.getCurrentDate()
          
        })
       
    },
    saveNewStaff(){
        let sex
        if(this.data.sexSelect == "女" ){
            sex= 0
       }else if(this.data.sexSelect == "男"){
            sex= 1
       }else{
           sex=""
       }
        let birthday=this.data.date
        let gender= sex  
        let employeeName=this.data.name
        let employeePhone=this.data.phone
        let education=(this.data.educationSelect!=="请选择日历")?this.data.educationSelect:""
        let employeeId=app.globalData.newAddStaff.employeeId||""

        new newAddStaff(employeeName,employeePhone,gender,birthday,education,employeeId).then(data=>{
             
            console.log("data",data)
            let type= this.data.type
            
	
            addEmployee(data,type).then(res=>{
                 wx.showToast({
                     icon:"none",
                     duration:2000,
                     title:"保存成功"
                 })
                 app.globalData.newAddStaff={}
                wx.navigateBack()
            })

        }).catch(()=>{})

        console.log("res", app.globalData.newAddStaff)
    },



    PickerChange1(e){
             // 返回的是 数组的下标
             if(e.detail.value == 0 ){
                this.setData({
                    sexSelect: "女"
                  })
           }else if(e.detail.value == 1){
            this.setData({
                sexSelect: "男"
              })
           }else{
            this.setData({
                sexSelect:"请选择性别"
              })
           }


           
              app.globalData.newAddStaff.gender=e.detail.value
       
    },
    PickerChange2(e){
        // 返回的是 数组的下标
          this.setData({
            educationSelect:this.data.education[e.detail.value]
          })
          
          app.globalData.newAddStaff.education=this.data.education[e.detail.value]
    },
    DateChange(e){
        this.setData({
            date: e.detail.value
          })
          app.globalData.newAddStaff.birthday=e.detail.value 
    },
    bindInputName(e){

        this.setData({
            name:filteremoji(e.detail.value)   
          })
          app.globalData.newAddStaff.employeeName=e.detail.value 
    },
    bindInputPhone(e){
        this.setData({
            phone:e.detail.value
          })
          app.globalData.newAddStaff.employeePhone=e.detail.value 
    }
})